

{% if criteo_pixels %}
    <script type="text/javascript" src="https://dynamic.criteo.com/js/ld/ld.js?a={{criteo_pixels | join: ','}}" async></script>
    <script type = "text/javascript">
    
    (function(){
        var deviceType = /iPad/.test(navigator.userAgent) ? "t" : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent) ? "m" : "d";
        window.doCriteoQ = function () {
            window.criteo_q = window.criteo_q || [];
            {% for pixel in criteo_pixels %}
                window.criteo_q.push({ event: "setAccount", account: {{pixel}} });
            {% endfor %}
            window.criteo_q.push({ event: "setRetailerVisitorId", id: _GET_C_SETTING_("global_visit_id")});
            window.criteo_q.push({ event: "setSiteType", type: deviceType});
            {% if pixel_user.email %}
                window.criteo_q.push({ event: "setEmail", email:"{{pixel_user.email | string_sha256}}", hash_method: "sha256" });
            {% endif %}
            window.criteo_q.push(...arguments);
        };
        
        window['TOP_GLOBAL_CALLBACK_LAYER'].register({
            'page_view': function() {
                {%- if template == 'index' -%}
                    doCriteoQ({event:'viewHome'});
                {%- else -%}
                    doCriteoQ({event:'viewPage'});
                {%- endif -%}
            },
            'collection_detail':function(collection){
                var product_spu= collection.products.map(item => item.spu);
                doCriteoQ({event:'viewList', item: product_spu });
            },
            'cart_list':function(cart){
                let product_spu = [].map(item =>{
                    return {
                        id:item.product.spu,
                        price:item.price,
                        quantity:item.quantity
                        }
                });
                doCriteoQ({event: 'viewBasket',item: product_spu });
            },
            'view_content': function(product) {
                doCriteoQ({event:'viewItem',item:product.spu,price: product.variant.price, availability: 1});
            },
            'search': function(value,products) {
                var product_spu= products.map(item => item.spu);
                doCriteoQ({event:'viewList', item:product_spu,keywords:value});
            },
            'purchase': function(order) {
            {% if order.id %}
                var products = order.products.map(item => {
                    return {id: item.spu, price: item.discount_price, quantity: item.quantity};
                });
                {%- if order.customer_id  -%}
                    doCriteoQ({event:'trackTransaction', id: order.order_number, item:products},{event: "setEmail",email:order.customer_info.email});
                {% else %}
                    doCriteoQ({event:'trackTransaction', id:order.order_number, item:products});
                {%- endif -%}
            {% endif %}
            },
            'add_to_cart': function(ecdata) {
                doCriteoQ({event:'addToCart', item:[{id: ecdata['ec_data_spu'], quantity: ecdata['ec_data_quantity'], price: ecdata['ec_data_price']}]});
            }
        });
    })(window)
    </script>
{% endif %}
